package com.xyy.saas.payment.controller;

import com.alibaba.fastjson.JSON;
import com.xyy.saas.payment.api.impl.PaymentOrderApiImpl;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.cores.vo.OrderRefundVoV1;
import com.xyy.saas.payment.cores.vo.Pay4noCashierVoV1;
import com.xyy.saas.payment.cores.vo.ResultVO;
import com.xyy.saas.payment.payment.core.dto.AppPayDto;
import com.xyy.saas.payment.payment.core.dto.MiniPayDto;
import com.xyy.saas.payment.payment.core.dto.PayDto;
import com.xyy.saas.payment.payment.core.service.PaymentService;
import com.xyy.saas.payment.payment.core.vo.WithdrawalVO;
import com.xyy.saas.payment.util.JSONUtils;
import com.xyy.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author chenshuyi
 */
/** 交易接口 */
@RestController
@RequestMapping(value = {"/api/payment"})
public class PaymentController {
    private static Logger LOGGER = LoggerFactory.getLogger(PaymentController.class);
    @Resource
    private PaymentService paymentService;
    @Resource
    private PaymentOrderApiImpl paymentOrderApi;


    /** app支付接口 */
    @RequestMapping(value = {"/pay/app"}, method = {RequestMethod.POST})
    public ResultVO<WithdrawalVO> appPay(@RequestParam String businessOrderNo,
                                         @RequestParam String channel,
                                         @RequestParam String body,
                                         @RequestParam String extra){
        ResultVO resultVO = new ResultVO();
        LOGGER.info("PaymentController appPay begin businessOrderNo:{}, channel:{}, body:{}, extra:{}", businessOrderNo, channel, body, extra);
        try{
            if (StringUtil.isEmpty(businessOrderNo, channel, body)){
                LOGGER.warn("PaymentController appPay param error businessOrderNo:{}, channel:{}, body:{}, extra:{}", businessOrderNo, channel, body, extra);
                resultVO.setCode(ResultCodeEnum.PARAM_ERROR.getCode());
                resultVO.setMsg(ResultCodeEnum.PARAM_ERROR.getMsg());
                return resultVO;
            }
            AppPayDto appPayDto = new AppPayDto();
            appPayDto.setBusinessOrderNo(businessOrderNo);
            appPayDto.setChannel(channel);
            appPayDto.setBody(body);
            if (!StringUtil.isEmpty(extra)){
                appPayDto.setMap(JSONUtils.parse2Map(extra, String.class, Object.class));
            }
            resultVO = paymentService.appPay(appPayDto);
        }catch (Exception e){
            LOGGER.error("PaymentController appPay error businessOrderNo:{}, channel:{}, body:{}, extra:{}", businessOrderNo, channel, body, extra, e);
            resultVO.setCode(ResultCodeEnum.DEFAULT_ERROR.getCode());
            resultVO.setMsg(ResultCodeEnum.DEFAULT_ERROR.getMsg());
            return resultVO;
        }
        LOGGER.info("PaymentController appPay end businessOrderNo:{}, resultVO:{}", businessOrderNo, JSON.toJSON(resultVO));
        return resultVO;
    }

    /** 小程序支付接口 */
    @RequestMapping(value = {"/pay/mini"}, method = {RequestMethod.POST})
    public ResultVO<WithdrawalVO> miniAppPay(@RequestParam String businessOrderNo,
                                             @RequestParam String channel,
                                             @RequestParam String body,
                                             @RequestParam String extra){
        ResultVO resultVO = new ResultVO();
        try{
            if (StringUtil.isEmpty(businessOrderNo, channel, body)){
                LOGGER.warn("PaymentController miniAppPay param error businessOrderNo:{}, channel:{}, body:{}, extra:{}", businessOrderNo, channel, body, extra);
                resultVO.setCode(ResultCodeEnum.PARAM_ERROR.getCode());
                resultVO.setMsg(ResultCodeEnum.PARAM_ERROR.getMsg());
                return resultVO;
            }
            MiniPayDto miniPayDto = new MiniPayDto();
            miniPayDto.setBusinessOrderNo(businessOrderNo);
            miniPayDto.setChannel(channel);
            miniPayDto.setBody(body);
            if (!StringUtil.isEmpty(extra)){
                miniPayDto.setMap(JSONUtils.parse2Map(extra, String.class, Object.class));
            }
            resultVO = paymentService.miniPay(miniPayDto);
        }catch (Exception e){
            LOGGER.error("PaymentController miniAppPay error businessOrderNo:{}, channel:{}, body:{}, extra:{}", businessOrderNo, channel, body, extra, e);
            resultVO.setCode(ResultCodeEnum.DEFAULT_ERROR.getCode());
            resultVO.setMsg(ResultCodeEnum.DEFAULT_ERROR.getMsg());
            return resultVO;
        }
        LOGGER.info("PaymentController miniAppPay end businessOrderNo:{}, resultVO:{}", businessOrderNo, JSON.toJSON(resultVO));
        return resultVO;
    }

    /** 无收银台下单支付 */
    @RequestMapping(value = "/pay4noCashier", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String pay4noCashier(@RequestBody Pay4noCashierVoV1 param) {
        return JSON.toJSONString(paymentOrderApi.pay4noCashier(param, ""));
    }

    /** 退款 */
    @PostMapping(value = "/refund", produces = "application/json;charset=UTF-8")
    public String refund(@RequestBody OrderRefundVoV1 orderRefundVo) {
        return JSON.toJSONString(paymentOrderApi.refund(orderRefundVo, ""));
    }

    /** 无收银台下单支付 */
    @RequestMapping(value = "/channelDegradedPay", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String pay4noCashier(@RequestBody PayDto param) throws PaymentException {
        return JSON.toJSONString(paymentService.channelDegradedPay(param));
    }

}
